/*
** my_string_to_wordtab.c for  in /home/georgi_v/rendu/PSU_2013_minishell1/libs
**
** Made by georgi_v
** Login   <georgi_v@epitech.net>
**
** Started on  Wed Nov  6 21:05:46 2013 georgi_v
** Last update Fri Dec  6 19:55:24 2013 georgi_v
*/

#include "my.h"

void	str_cpy(char *destination, char *source, int size)
{
  int	cnt;

  cnt = 0;
  while (size > 0)
    {
      destination[cnt] = source[cnt];
      cnt++;
      size--;
    }
  destination[cnt] = '\0';
}

int	word_count(char *str, char separator)
{
  int	cnt;

  cnt = 0;
  while (*str != '\0')
    {
      if (*str == separator)
	str = str + 1;
      else
	{
	  while (*str != '\0' && *str != separator)
	    str = str + 1;
	  cnt = cnt + 1;
	}
    }
  return (cnt);
}

char	**my_str_to_wordtab(char *str, char separator)
{
  char	**the_list;
  int	words;
  int	cnt_malloc;
  int	cnt;

  words = word_count(str, separator);
  if ((the_list = malloc((words + 1) * sizeof(char*))) == NULL)
    return (NULL);
  cnt_malloc = 0;
  while (*str != '\0')
    {
      if (*str == separator)
	str++;
      else
	{
	  cnt = 0;
	  while (str[cnt] != '\0' && str[cnt] != separator)
	    cnt++;
	  the_list[cnt_malloc] = malloc(cnt * sizeof(char));
	  str_cpy(the_list[cnt_malloc], str, cnt);
	  str = str + cnt;
	  cnt_malloc++;
	}
    }
  the_list[cnt_malloc] = NULL;
  return (the_list);
}
